[#ftl]
[#import "common/macrolibs.ftl" as lib]
[#import "common/commonclass.ftl" as cls]
package ${lib.packageBase}.business;

[#if (clazz.mtmRelations?size>0)]
import java.util.ArrayList;
import java.util.List;
[/#if]
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

[#assign dateFlag=false]
[#list clazz.dynaAttributes as attr][#if (attr.type>5)&&(attr.type<9)]
[#if (!dateFlag)]
import java.util.Date;
[#assign dateFlag=true]
[/#if]
[/#if][/#list]

import com.smzd.core.business.AbstractService;
import com.smzd.core.dao.IDao;
[#if (clazz.mtmRelations?size>0)]
import com.smzd.core.domain.Many2Many;
[/#if]
import com.smzd.core.utils.json.FGRowConverter;
import ${lib.packageBase}.dao.${cls.mySimpleClassName}Dao;
import ${lib.packageBase}.domain.${cls.mySimpleClassName};
[#assign classMap={}][#assign sysVarUsageMap={"0":false, "1": false, "2": false, "3": false}]
[#list clazz.dynaAttributes as attr][#switch attr.type][#case 9]
[#if !classMap["${attr.refClass.id}"]??&&(attr.refClass.id!=clazz.id)]
import ${lib.packageBase}.domain.${cls.myRefClassNames["${attr.id}"]};
[#assign classMap=classMap+{"${attr.refClass.id}", true}][/#if]
[#break]
[#case 10][#if !sysVarUsageMap["1"]]
import com.smzd.framework.system.domain.Meeting;
[#assign sysVarUsageMap=sysVarUsageMap+{"1": true}][/#if][#break]
[#case 11][#if !sysVarUsageMap["2"]]
import com.smzd.framework.system.domain.Group;
[#assign sysVarUsageMap=sysVarUsageMap+{"2": true}][/#if][#break]
[#case 12][#if !sysVarUsageMap["3"]]
import com.smzd.framework.system.domain.User;
[#assign sysVarUsageMap=sysVarUsageMap+{"3": true}][/#if][#break]
[#default][/#switch][/#list]
[#if dateFlag]
import com.smzd.framework.utils.SimpleDateTimeUtil;
[/#if]
[#if (clazz.mtmRelations?size>0)]
[#if clazz.scopeClass??]
import ${lib.packageBase}.domain.${cls.myScopeClassName};
[/#if]
import com.smzd.core.utils.db.${cls.myPageName};
[@lib.importOwnerClass level=clazz.ownerLevel/]

[/#if]

@Service
public class ${cls.mySimpleClassName}Service extends AbstractService<${cls.mySimpleClassName}> {
    @Autowired
    private ${cls.mySimpleClassName}Dao dao;
    
    @Override
    protected IDao<${cls.mySimpleClassName}> getDao() {
        return dao;
    }
    
    @Override
    protected FGRowConverter<${cls.mySimpleClassName}> getConverter() {
        return converter;
    }

    private static FGRowConverter<${cls.mySimpleClassName}> converter = new FGRowConverter<${cls.mySimpleClassName}>() {
        @Override
        public Object[] convert(${cls.mySimpleClassName} elem) {
            [#list clazz.dynaAttributes as attr]
            [#if !attr.carrier][#if attr.type>8]
            ${cls.myAttrJavaTypes["${attr.id}"]} ${cls.myAttrNames["${attr.id}"]} = elem.${cls.myGetters["${attr.id}"]}();
            Object[] obj_${cls.myAttrNames["${attr.id}"]};
            if(${cls.myAttrNames["${attr.id}"]}==null){
                obj_${cls.myAttrNames["${attr.id}"]} = null;
            }else{
                Integer id_${cls.myAttrNames["${attr.id}"]} = ${cls.myAttrNames["${attr.id}"]}.getId();
                String name_${cls.myAttrNames["${attr.id}"]} = ${cls.myAttrNames["${attr.id}"]}.getName();
                obj_${cls.myAttrNames["${attr.id}"]} = new Object[]{id_${cls.myAttrNames["${attr.id}"]}, name_${cls.myAttrNames["${attr.id}"]}};
            }
            [#else][#if (attr.type>5)&&(attr.type<9)]
            Date ${cls.myAttrNames["${attr.id}"]} = elem.${cls.myGetters["${attr.id}"]}();
            String str_${cls.myAttrNames["${attr.id}"]} = null;
            if(${cls.myAttrNames["${attr.id}"]}!=null){
                str_${cls.myAttrNames["${attr.id}"]} = SimpleDateTimeUtil.formatToDate[#if attr.type==8]Time[/#if](${cls.myAttrNames["${attr.id}"]});
            }
            [/#if][/#if][/#if][/#list]
            Object[] fs = new Object[]{
                    elem.getId(),
                    elem.getName(),
                    elem.getDescription()[#list clazz.dynaAttributes as attr][#if !attr.carrier],
                    [#if attr.type>8]obj_${cls.myAttrNames["${attr.id}"]}[#else][#if 
                    (attr.type>5)&&(attr.type<9)]str_${cls.myAttrNames["${attr.id}"]}[#else][#if 
                    attr.type<6]elem.${cls.myGetters["${attr.id}"]}()[/#if][/#if][/#if][/#if][/#list]
                };
                return fs;
        }
    };
    [#if (clazz.mtmRelations?size>0)]
    private static FGRowConverter<${cls.mySimpleClassName}> checkedConverter = new FGRowConverter<${cls.mySimpleClassName}>() {
        @Override
        public Object[] convert(${cls.mySimpleClassName} elem) {
            Object[] fields = new Object[] { 
                    elem.getId(), 
                    elem.getName(),
                    elem.getDescription(),
                    elem.getChecked()
            };
            return fields;
        }
    };

    [#list clazz.mtmRelations as mtm]
    [#assign masterClassName][@lib.simpleClassName clazz=mtm.master/][/#assign]
    [#assign slaveClassName][@lib.simpleClassName clazz=mtm.slave/][/#assign]
    [#assign configView][@lib.moduleName clazz=mtm.master/][@lib.moduleNames clazz=mtm.slave/].ftl[/#assign]
    [#assign masterId]${masterClassName?uncap_first}Id[/#assign]
    [#assign slaveId]${cls.mySimpleClassName?uncap_first}Id[/#assign]
    public String getOuter${slaveClassName}ListBy${masterClassName} (${cls.myPageName}${cls.myPageParam} page) {
        dao.getOuter${slaveClassName}ListBy${masterClassName}(page);
        return page.toFlexiGridJsonString(checkedConverter);
    }
    
    public void save${masterClassName}${slaveClassName}Relations (int ${masterId}, String ids) {
        List<Many2Many> toAdd = new ArrayList<Many2Many>();
        List<Many2Many> toDel = new ArrayList<Many2Many>();
        for(String id : ids.split(",")){
            int ${slaveId} = Integer.parseInt(id);
            Many2Many mm = new Many2Many();
            mm.setKey1(${masterId});
            if(${slaveId}<0){
                mm.setKey2(-${slaveId});
                toDel.add(mm);
            }else{
                mm.setKey2(${slaveId});
                toAdd.add(mm);
            }
        }
        dao.save${masterClassName}${slaveClassName}Relations(toAdd, toDel);
    }
    [/#list]
    [/#if]
    
}